package com.bangcommunity.bbframe.sdm.web.taglib.page;

import com.bangcommunity.bbframe.common.base.CommonList;

/**
 * Created by IntelliJ IDEA. User: joeytang Date: 2015-12-29 13:54
 * 
 * 共[x]条记录,1/8页 第一页 上一页 1 2 3 4 5 下一页 最末页
 */
public class PagesModel12 extends AbstractPagesModel {

    /**
     * 根据 commonlist和pagenum生成分页html代码
     * 
     * @param cl
     *            包含分页需要的信息
     * @param pageNum
     *            限制每屏显示的页码个数 ，如：上一页 1 2 3 4 5 下一页该值表示中间显示多少个页码
     * @return
     */
    @Override
    public String genPageHtml(CommonList cl, int pageNum, String op) {
        StringBuffer buffer = new StringBuffer();
        // buffer.append("共[" + cl.pageNo + "/" + cl.pageNum + "]页" + "&nbsp;");

        buffer.append("<div class=\"span6\">");
        buffer.append("<label class=\"pull-left\">显示第 ");
        int begin = Math.max((cl.pageNo - 1) * cl.pageSize + 1, 1);
        int end = Math.min(cl.getRecNum(), begin + cl.pageSize - 1);
        buffer.append(begin);
        buffer.append(" - ");
        buffer.append(end);
        buffer.append(" 条记录，共 ");
        buffer.append(cl.getRecNum());
        buffer.append(" 条</label>");
        buffer.append("</div>");
        buffer.append("<div class=\"span6 pull-right\">");
        buffer.append("<ul class=\"pagination pull-right\">");
        if (cl.pageNo == 1) {
            buffer.append("<li class='disabled'><span>第一页</span></li>");
            buffer.append("<li class='disabled'><span>上一页</span></li>");
        } else if (cl.pageNo > 1) {
            buffer.append(" <li><a " + getPageFun(1, cl.uri, cl.target, op, cl.searchStr) + " >第一页</a></li>");
            buffer.append(
                    "<li> <a " + getPageFun(cl.pageNo - 1, cl.uri, cl.target, op, cl.searchStr) + " >上一页</a></li>");
        }
        int currentNum = (cl.pageNo % pageNum == 0 ? (cl.pageNo / pageNum) - 1 : (int) (cl.pageNo / pageNum)) * pageNum;
        if (currentNum < 0)
            currentNum = 0;
        if (cl.pageNo > pageNum) {
            buffer.append("<li> <a " + getPageFun(currentNum - pageNum + 1, cl.uri, cl.target, op, cl.searchStr)
                    + ">...</a></li>");
        }

        // ---------------------------------
        // 显示中间可用的翻页码
        // ---------------------------------
        for (int i = 0; i < pageNum; i++) {
            if ((currentNum + i + 1) > cl.pageNum || cl.pageNum < 2)
                break;
            if (currentNum + i + 1 == cl.pageNo) {
                buffer.append("<li class='active'><span>" + (currentNum + i + 1) + "</span></li>");
            } else {
                buffer.append(" <li><a " + getPageFun(currentNum + i + 1, cl.uri, cl.target, op, cl.searchStr) + ">");
                buffer.append((currentNum + i + 1) + "");
                buffer.append("</a></li>");
            }
        }

        if (cl.pageNum > (currentNum + pageNum)) {
            buffer.append(" <li><a " + getPageFun(currentNum + 1 + pageNum, cl.uri, cl.target, op, cl.searchStr)
                    + ">...</a></li>");
        }

        if (cl.pageNo == cl.pageNum) {
            buffer.append("<li class='disabled'><span>下一页</span></li>");
            buffer.append("<li class='disabled'><span>最末页</span></li>");
        } else if (cl.pageNo < cl.pageNum) {
            buffer.append(
                    " <li><a " + getPageFun(cl.pageNo + 1, cl.uri, cl.target, op, cl.searchStr) + " >下一页</a></li>");
            buffer.append(" <li><a " + getPageFun(cl.pageNum, cl.uri, cl.target, op, cl.searchStr) + " >最末页</a></li>");
        }
        buffer.append("</ul>");
        buffer.append("</div>");
        return buffer.toString();
    }

}
